package org.vectrics.mail.struts;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.jsp.JspException;

import org.apache.log4j.Level;
import org.vectrics.common.http.HttpUtils;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.Request;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;
import org.vectrics.user.Person;

public class StageBulkEmailAction extends VectricsStrutsAction {
	
	public StageBulkEmailAction() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	protected String doExecute(VectricsActionData actionData)
			throws JspException {
		Request request = actionData.getRequest();
		HttpUtils.logRequestInfo(request, log, Level.INFO);		
		Date stageDate = new Date();
		
		String templateId = request.getParameter("templateId");
		actionData.getRequest().setAttribute("templateId", templateId);

		String categoryId = request.getParameter("categoryId");
		actionData.getRequest().setAttribute("categoryId", categoryId);
		
		String templateText = request.getParameter("templateText");
		Long senderId = new Long(request.getParameter("sender_id"));
		Person sender = CoreServiceLocator.getPersonService().findPersonById(senderId);

		String subject = request.getParameter("subject");
		
		LetterStagingManager stagingManager = new LetterStagingManager(sender, stageDate);
		stagingManager.saveTemplateToStagingDirectory(templateText);
		stagingManager.saveSubject(subject);
		actionData.getRequest().setAttribute("stagingDirectory", stagingManager.getStagingDirectoryFullPath());
		actionData.getRequest().setAttribute("stageDateMillis", "" + stageDate.getTime());
		actionData.getRequest().setAttribute("sender", sender);
		actionData.getRequest().setAttribute("subject", subject);
		
		GetRequestTemplateParameterLoader getLoader = new GetRequestTemplateParameterLoader();
		//TemplateParameterLoader templateParameterLoader = getLoader.load(request);
		

		Set<String> paramNames = request.getParameterNames();
		Iterator iter = paramNames.iterator();
		while (iter.hasNext()) {
			String paramName = (String)iter.next();
			if (paramName.startsWith("recip_")) {
				Map<String, Object> commonReportParameterMap = new HashMap<String, Object>();
				addCustomObjectsToMap(request, commonReportParameterMap);

				String value = request.getParameter(paramName);
				Long recipPersonId = new Long(value);
				Person recipient = CoreServiceLocator.getPersonService().findPersonById(recipPersonId);
				stagingManager.createEmailToPerson(recipient, commonReportParameterMap);
				commonReportParameterMap.put("recipient", ObjectGraph.personToTemplateObject(recipient));
			}
		}
		return "success";
	}

	/**
	 * 
	 * @param request
	 * @param commonReportParameterMap
	 */
	private void addCustomObjectsToMap( 
			Request request, Map<String, Object> parameterMap) {
		ObjectGraph objectGraph = (ObjectGraph)request
				.getSessionAttribute("freemarkerObjectGraph");
		
		Set<String> keys = objectGraph.getKeys();
		Iterator<String> keyIter = keys.iterator();
		while (keyIter.hasNext()) {
			String key = keyIter.next();
			TemplateObject templateObject = objectGraph.getObject(key);
			parameterMap.put(key, templateObject);
			log.info("Added object to template map: " + key);
			//templateParameterLoader.loadParameterObject(key, templateObject, parameterMap);
		}
		
		/*
		Enumeration paramNames = request.getParameterNames();
		while (paramNames.hasMoreElements()) {
			String paramName = (String)paramNames.nextElement();
			if (paramName.startsWith("tmplt_")) {
				String parameterValue = request.getParameter(paramName);
				String adjustedParam = paramName.substring("tmplt_".length());
				templateParameterLoader.loadParameterObject(adjustedParam, parameterValue, parameterMap);
			}
		}
		*/
	}	
	
}
